# Read Data
setwd("C:/Users/eas24f/OneDrive - Florida State University/Research/Inertia RESTAT/Data/COVCAL Choice Sets") # Office computer directory
data <- read.csv("CA Entry-Exit.csv") # This is the raw data merged together in Excel


# Grab insurer_plans
insurer_plans <- colnames(data)[5:dim(data)[2]]

# 3-Digit Zips	

	# Create identifiers
	data$zip3 <- as.integer(substr(data$Zip,1,3))
	data$zip3_region_year <- paste(data$zip3,data$Region,data$Year,sep="_")
	plan_ids <- unique(data$zip3_region_year)

	# Populate choice matrix

	data3 <- matrix(NA,length(plan_ids),length(insurer_plans),dimnames=list(plan_ids,insurer_plans))
	for(i in rownames(data3)) {
		data3[i,] <- pmin(1,colSums(data[data$zip3_region_year == i,insurer_plans],na.rm=TRUE))
	}

	# Add identifiers

	names10 <- which(nchar(rownames(data3)) == 10)
	names11 <- which(nchar(rownames(data3)) == 11)

	identifiers <- matrix(0,nrow(data3),3,dimnames=list(rownames(data3),c("zip3","region","year")))
	identifiers[,"zip3"] <- as.numeric(substr(rownames(data3),1,3))
	identifiers[names10,"region"] <- as.numeric(substr(rownames(data3)[names10],5,5))
	identifiers[names11,"region"] <- as.numeric(substr(rownames(data3)[names11],5,6))
	identifiers[names10,"year"] <- as.numeric(substr(rownames(data3)[names10],7,10))
	identifiers[names11,"year"] <- as.numeric(substr(rownames(data3)[names11],8,11))

	data3 <- cbind(identifiers,data3)
	write.csv(data3,"choice_set.csv")


# Counties
	
	setwd("C:/Users/eas24f/OneDrive - Florida State University/Research/Inertia RESTAT/Data") 
	
	data$Western_Health <- data$Western
	insurers <- c("Anthem","Blue_Shield","Chinese_Community","Contra_Costa","Health_Net","Kaiser","LA_Care","Molina","Oscar","Sharp","United","Valley","Western_Health")
	
	for(t in 2014:2019) {
			
		data_year <- data[data$Year == t,]	
		data_year <- data_year[!duplicated(data_year$County),]	
		
		output <- data_year[,c("County","Region")]
		output <- rbind(output,c("LOS ANGELES2",16))
		output[output$County == "LOS ANGELES","County"] <- "LOS ANGELES1"
		output <- output[order(output$County),]
		colnames(output) <- c("Counties","Rating_Area")
		rownames(output) <- output$Counties
		output[,insurers] <- 0
		
		for(m in rownames(output)) {
			if(m == "LOS ANGELES1") {
				data_county <- data[data$County == "LOS ANGELES" & data$Year == t & data$Region == 15,]	
			} else if(m == "LOS ANGELES2") {
				data_county <- data[data$County == "LOS ANGELES" & data$Year == t & data$Region == 16,]	
			} else {
				data_county <- data[data$County == m & data$Year == t,]	
			}
			output[m,"Anthem"] <- pmin(1,sum(data_county[,c("Anthem_EPO","Anthem_HMO","Anthem_PPO","Anthem_EPO_MSP","Anthem_PPO_MSP")],na.rm=TRUE))
			output[m,"Blue_Shield"] <-  pmin(1,sum(data_county[,c("Blue_Shield_HMO","Blue_Shield_EPO","Blue_Shield_PPO")],na.rm=TRUE))
			output[m,"Health_Net"] <-  pmin(1,sum(data_county[,c("Health_Net_HMO","Health_Net_HSP","Health_Net_EPO","Health_Net_PPO")],na.rm=TRUE))
			output[m,"Sharp"] <-  pmin(1,sum(data_county[,c("Sharp1","Sharp2")],na.rm=TRUE))
			output[m,"Chinese_Community"] <- pmin(1,sum(data_county[,"Chinese_Community"],na.rm=TRUE))
			output[m,"Contra_Costa"] <- pmin(1,sum(data_county[,"Contra_Costa"],na.rm=TRUE))
			output[m,"Kaiser"] <- pmin(1,sum(data_county[,"Kaiser"],na.rm=TRUE))
			output[m,"LA_Care"] <- pmin(1,sum(data_county[,"LA_Care"],na.rm=TRUE))
			output[m,"Molina"] <- pmin(1,sum(data_county[,"Molina"],na.rm=TRUE))
			output[m,"Oscar"] <- pmin(1,sum(data_county[,"Oscar"],na.rm=TRUE))
			output[m,"United"] <- pmin(1,sum(data_county[,"United"],na.rm=TRUE))
			output[m,"Valley"] <- pmin(1,sum(data_county[,"Valley"],na.rm=TRUE))
			output[m,"Western_Health"] <- pmin(1,sum(data_county[,"Western_Health"],na.rm=TRUE))
		}
		

		# Remove Insurers Not Present
		counties_present <- colSums(output[,setdiff(colnames(output),c("Counties","Rating_Area"))])
		remove_insurers <- names(counties_present)[which(counties_present == 0)]
		output[,remove_insurers] <- NULL

		write.csv(output,paste("ca_counties_",t,".csv",sep=""))
	}

